<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\store\shop;

use app\common\model\store\shop\Clerk as ClerkModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 商家门店店员模型
 * Class Clerk
 * @package app\api\model\store\shop
 */
class Clerk extends ClerkModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time',
    ];

    public function getListClerk($userId)
    {
        // 查询构造器
        $query = $this->getNewQuery();

        // 获取门店列表数据
        $list = $query->with(['role', 'shop'])

            ->where('user_id', '=', $userId)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->select();

        return $list;
    }
    public function getList(int $shopId)
    {
        // 查询构造器
        $query = $this->getNewQuery();

        // 获取门店列表数据
        $list = $query->with(['user.avatar', 'role'])

            ->where('is_delete', '=', 0)
            ->where('shop_id', '=', $shopId)
            ->order(['role_id' => 'asc'])
            ->select();

        return $list;
    }
    /*
		 *渠道排行
	*/
    public function rankListYuyue(int $shopId, $date)
    {
        // 查询构造器
        $query  = $this->getNewQuery();
        $filter = [];
        if ($date == 1) {
            //本周
            $week = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") + 0, date("d") - date("w") + 1, date("Y") + 0));
            $time = strtotime($week);
        } elseif ($date == 2) {
            //本月
            $date = date('Y-m-t 23:59:59', strtotime('-1 month'));
            $time = strtotime($date);
        } else {
            $time = strtotime(Date('Y-m-d'));

        }
        $filter[] = ['create_time', '>=', $time];

        // 获取门店列表数据
        $list = $query->with(['user.avatar', 'role'])

            ->where('is_delete', '=', 0)
            ->where('shop_id', '=', $shopId)
            ->select();
        $newArr  = [];
        $sortArr = [];
        foreach ($list as $key => $value) {
            // $order = Db::name('ding_yuyue')
            // 	->where('clerk_id', $value['clerk_id'])
            // 	->where('is_delete', 0)
            // 	->where('status', '<>', 30)
            // 	->where($filter)->count();

            $order = Db::name('ding_order')
                ->where('clerk_id', $value['clerk_id'])
                ->where('is_delete', 0)
                ->where('shop_id', $shopId)
                ->where('status', '<>', 30)
                ->where($filter)->count();
            $value['order_num'] = $order;
            if ($order > 0) {
                $newArr[]  = $value;
                $sortArr[] = $order;
            }

        }

        array_multisort($sortArr, SORT_DESC, $newArr);
        return $newArr;
    }
    public function rankList(int $shopId, $date)
    {
        // 查询构造器
        $query  = $this->getNewQuery();
        $filter = [];
        if ($date == 1) {
            //本周
            $week = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") + 0, date("d") - date("w") + 1, date("Y") + 0));
            $time = strtotime($week);
        } elseif ($date == 2) {
            //本月
            $date = date('Y-m-t 23:59:59', strtotime('-1 month'));
            $time = strtotime($date);
        } else {
            $time = strtotime(Date('Y-m-d'));

        }
        $filter[] = ['create_time', '>=', $time];

        // 获取门店列表数据
        $list = $query->with(['user.avatar', 'role'])

            ->where('is_delete', '=', 0)
            ->where('shop_id', '=', $shopId)
            ->select();
        $newArr  = [];
        $sortArr = [];
        foreach ($list as $key => $value) {
            $order = Db::name('ding_order')
                ->where('clerk_id', $value['clerk_id'])
                ->where('is_delete', 0)
                ->where('status', '<>', 30)
                ->where($filter)->count();
            if ($order > 0) {
                $value['order_num'] = $order;
                $newArr[]           = $value;
                $sortArr[]          = $order;
            }

        }

        array_multisort($sortArr, SORT_DESC, $newArr);

        return $newArr;
    }
    /**
     * 获取店员信息
     * @param int $userId
     * @return Clerk|array|null
     * @throws BaseException
     */
    public static function getClerkInfo(int $userId)
    {
        // 获取店员记录
        $clerk = static::detail(['user_id' => $userId, 'is_delete' => 0]);
        if (empty($clerk)) {
            throwError('很抱歉，您不是门店的店员');
        }
        if (! $clerk['status']) {
            throwError('很抱歉，当前店员状态已被禁用');
        }
        return $clerk;
    }
    public function edit($data)
    {
        $this->save($data);

        return true;
    }
    /**
     * 验证店员信息是否为指定门店的核销员
     * @param Clerk $clerk 店员信息
     * @param int $shopId 指定的门店ID
     * @return bool
     * @throws BaseException
     */
    public static function checkAuthority(self $clerk, int $shopId): bool
    {
        if ($clerk['shop_id'] != $shopId) {
            // throwError('很抱歉，当前店员不属于该门店，没有核销权限');
        }
        if (! $clerk['status']) {
            throwError('很抱歉，当前店员状态已被禁用');
        }
        return true;
    }
}
